﻿<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Dotway.WPF.Controls"
    xmlns:converters="clr-namespace:Dotway.WPF.Controls.Converters">

    <converters:ColorToHexConverter x:Key="colorToHexConverter" />
    <converters:RgbToColorMultiConverter x:Key="rgbToColorMultiConverter" />

    <DrawingBrush x:Key="chessBrush" Viewport="0,0,0.1,0.6" TileMode="Tile">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="#66FFFFFF">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <RectangleGeometry Rect="0,0,5,5"/>
                            <RectangleGeometry Rect="5,5,5,5"/>
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="#66000000">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <RectangleGeometry Rect="5,0,5,5"/>
                            <RectangleGeometry Rect="0,5,5,5"/>
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
    
    <DrawingBrush x:Key="smallChessBrush" Viewport="0,0,0.1,2" TileMode="Tile">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing Brush="#66FFFFFF">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <RectangleGeometry Rect="0,0,5,5"/>
                            <RectangleGeometry Rect="5,5,5,5"/>
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="#66000000">
                    <GeometryDrawing.Geometry>
                        <GeometryGroup>
                            <RectangleGeometry Rect="5,0,5,5"/>
                            <RectangleGeometry Rect="0,5,5,5"/>
                        </GeometryGroup>
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>

    <Style TargetType="{x:Type local:ColorPicker}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ColorPicker}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}" Padding="10" CornerRadius="5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*" />
                                <RowDefinition Height="30" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <local:ColorMap x:Name="pickerColorMap" Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" SelectedColor="{Binding Path=SelectedColor, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" HColor="{Binding ElementName=pickerColorBar, Path=HColor, Mode=TwoWay}" BorderBrush="Black" BorderThickness="1"/>
                            <local:ColorBar x:Name="pickerColorBar" Grid.Column="2" Grid.Row="0" Orientation="Vertical" Width="40" VerticalAlignment="Stretch" BorderBrush="Black" BorderThickness="1" />

                            <Border x:Name="initialColor" Grid.Column="0" Grid.Row="1" BorderBrush="Black" BorderThickness="1" CornerRadius="5" Background="{StaticResource chessBrush}">
                                <Border.ToolTip>
                                    <TextBlock Text="Initial Color" />
                                </Border.ToolTip>
                                <Rectangle RadiusX="5" RadiusY="5" Margin="-0.5,-0.5,-0.5,-0.5">
                                    <Rectangle.Fill>
                                        <SolidColorBrush Color="{Binding Path=InitialColor, RelativeSource={RelativeSource TemplatedParent}}" />
                                    </Rectangle.Fill>
                                </Rectangle>
                            </Border>

                            <Border x:Name="currentColor" Grid.Column="1" Grid.Row="1" BorderBrush="Black" BorderThickness="1" CornerRadius="5" Background="{StaticResource chessBrush}">
                                <Border.ToolTip>
                                    <TextBlock Text="Current Color" />
                                </Border.ToolTip>
                                <Rectangle RadiusX="5" RadiusY="5" Margin="-0.5,-0.5,-0.5,-0.5">
                                    <Rectangle.Fill>
                                        <SolidColorBrush Color="{Binding ElementName=pickerColorMap, Path=SelectedColor.Color}" />
                                    </Rectangle.Fill>
                                </Rectangle>
                            </Border>

                            <StackPanel Grid.Column="3" Grid.Row="0" Margin="10,5,10,5">
                                <StackPanel Orientation="Horizontal" Margin="5">
                                    <TextBlock Text="R" Foreground="{TemplateBinding Foreground}"  TextDecorations="Underline" VerticalAlignment="Center" Margin="0,0,5,0" />
                                    <Grid>
                                        <local:EditSlider x:Name="redSlider" Minimum="0" Maximum="255" Width="90" Value="{Binding ElementName=pickerColorMap, Path=SelectedColor.R, Mode=TwoWay}"/>
                                        <Border CornerRadius="0,0,5,5" Height="3" VerticalAlignment="Bottom" Margin="1">
                                            <Border.Background>
                                                <LinearGradientBrush EndPoint="1.0,0.5" StartPoint="0.0,0.5">
                                                    <GradientStop Offset="0.0">
                                                        <GradientStop.Color>
                                                            <MultiBinding Converter="{StaticResource rgbToColorMultiConverter}" ConverterParameter="255 0 G B">
                                                                <MultiBinding.Bindings>
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.G" />
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.B" />
                                                                </MultiBinding.Bindings>
                                                            </MultiBinding>
                                                        </GradientStop.Color>
                                                    </GradientStop>
                                                    <GradientStop Offset="1.0">
                                                        <GradientStop.Color>
                                                            <MultiBinding Converter="{StaticResource rgbToColorMultiConverter}" ConverterParameter="255 255 G B">
                                                                <MultiBinding.Bindings>
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.G" />
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.B" />
                                                                </MultiBinding.Bindings>
                                                            </MultiBinding>
                                                        </GradientStop.Color>
                                                    </GradientStop>
                                                </LinearGradientBrush>
                                            </Border.Background>
                                        </Border>
                                    </Grid>
                                </StackPanel>

                                <StackPanel Orientation="Horizontal" Margin="5">
                                    <TextBlock Text="G" Foreground="{TemplateBinding Foreground}" TextDecorations="Underline" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <Grid>
                                        <local:EditSlider x:Name="greenSlider" Minimum="0" Maximum="255" Width="90" Value="{Binding ElementName=pickerColorMap, Path=SelectedColor.G, Mode=TwoWay}"/>
                                        <Border CornerRadius="0,0,5,5" Height="3" VerticalAlignment="Bottom" Margin="1">
                                            <Border.Background>
                                                <LinearGradientBrush EndPoint="1.0,0.5" StartPoint="0.0,0.5">
                                                    <GradientStop Offset="0.0">
                                                        <GradientStop.Color>
                                                            <MultiBinding Converter="{StaticResource rgbToColorMultiConverter}" ConverterParameter="255 R 0 B">
                                                                <MultiBinding.Bindings>
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.R" />
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.B" />
                                                                </MultiBinding.Bindings>
                                                            </MultiBinding>
                                                        </GradientStop.Color>
                                                    </GradientStop>
                                                    <GradientStop Offset="1.0">
                                                        <GradientStop.Color>
                                                            <MultiBinding Converter="{StaticResource rgbToColorMultiConverter}" ConverterParameter="255 R 255 B">
                                                                <MultiBinding.Bindings>
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.R" />
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.B" />
                                                                </MultiBinding.Bindings>
                                                            </MultiBinding>
                                                        </GradientStop.Color>
                                                    </GradientStop>
                                                </LinearGradientBrush>
                                            </Border.Background>
                                        </Border>
                                    </Grid>
                                </StackPanel>

                                <StackPanel Orientation="Horizontal" Margin="5">
                                    <TextBlock Text="B" Foreground="{TemplateBinding Foreground}" TextDecorations="Underline" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <Grid>
                                        <local:EditSlider x:Name="blueSlider" Minimum="0" Maximum="255" Width="90" Value="{Binding ElementName=pickerColorMap, Path=SelectedColor.B, Mode=TwoWay}"/>
                                        <Border CornerRadius="0,0,5,5" Height="3" VerticalAlignment="Bottom" Margin="1">
                                            <Border.Background>
                                                <LinearGradientBrush EndPoint="1.0,0.5" StartPoint="0.0,0.5">
                                                    <GradientStop Offset="0.0">
                                                        <GradientStop.Color>
                                                            <MultiBinding Converter="{StaticResource rgbToColorMultiConverter}" ConverterParameter="255 R G 0">
                                                                <MultiBinding.Bindings>
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.R" />
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.G" />
                                                                </MultiBinding.Bindings>
                                                            </MultiBinding>
                                                        </GradientStop.Color>
                                                    </GradientStop>
                                                    <GradientStop Offset="1.0">
                                                        <GradientStop.Color>
                                                            <MultiBinding Converter="{StaticResource rgbToColorMultiConverter}" ConverterParameter="255 R G 255">
                                                                <MultiBinding.Bindings>
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.R" />
                                                                    <Binding ElementName="pickerColorMap" Path="SelectedColor.G" />
                                                                </MultiBinding.Bindings>
                                                            </MultiBinding>
                                                        </GradientStop.Color>
                                                    </GradientStop>
                                                </LinearGradientBrush>
                                            </Border.Background>
                                        </Border>
                                    </Grid>
                                </StackPanel>

                                <StackPanel Orientation="Horizontal" Margin="5">
                                    <TextBlock Text="A" Foreground="{TemplateBinding Foreground}" TextDecorations="Underline" VerticalAlignment="Center" Margin="0,0,5,0"/>
                                    <Grid>
                                        <local:EditSlider x:Name="alphaSlider" Minimum="0" Maximum="255" Width="90" Value="{Binding ElementName=pickerColorMap, Path=SelectedColor.A, Mode=TwoWay}"/>
                                        <Border CornerRadius="0,0,5,5" Height="3" VerticalAlignment="Bottom" Margin="1" Background="{StaticResource smallChessBrush}">
                                            <Border>
                                                <Border.Background>
                                                    <LinearGradientBrush EndPoint="1.0,0.5" StartPoint="0.0,0.5">
                                                        <GradientStop Offset="0.0" Color="#00000000"/>
                                                        <GradientStop Offset="1.0" Color="#FF000000"/>
                                                    </LinearGradientBrush>
                                                </Border.Background>
                                            </Border>
                                        </Border>
                                    </Grid>
                                </StackPanel>
                            </StackPanel>

                            <Border Grid.Column="3" Grid.Row="1" CornerRadius="5" BorderBrush="Black" Background="#FF373737" BorderThickness="1">
                                <TextBox x:Name="hexValue" Margin="5" Width="100" Foreground="{TemplateBinding Foreground}" Background="Transparent" BorderThickness="0" Text="{Binding ElementName=pickerColorMap, Path=SelectedColor.Color, Mode=TwoWay, Converter={StaticResource colorToHexConverter}}" TextAlignment="Center" FontSize="12" />
                            </Border>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>